package cn.zqh.auth.domain;

import cn.zqh.auth.mapper.UserMapper;
import cn.zqh.auth.pojo.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 登录用户信息
 * Created by macro on 2020/6/19.
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class UserPlus implements UserDetails {

    @Resource
    private UserMapper userMapper;


    /**
     * ID
     */
    private String id;
    /**
     * 用户名
     */
    private String qqEmail;
    /**
     * 用户密码
     */
    private String password;
    /**
     * 用户状态
     */
    private Boolean enabled;
    /**
     * 权限数据
     */
    private Collection<SimpleGrantedAuthority> authorities;

    public UserPlus() {

    }

    public UserPlus(User user,List<String> roleList) {
//        List<String> roleCodeByEmail = userMapper.getRoleCodeByEmail(user.getQqEmail());
//        System.out.println(roleCodeByEmail);
        this.setId(user.getUserId());
        this.setQqEmail(user.getQqEmail());
        this.setPassword(user.getPassword());
        this.setEnabled(user.getIsDelete() == 1);
        if (roleList != null) {
            authorities = new ArrayList<>();
            roleList.forEach(item -> authorities.add(new SimpleGrantedAuthority(item)));
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.qqEmail;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }


}
